<?php

namespace UI;

class Table {

	private $id = null;
	private $rows = array();
	private $header = array();
	private $att_rows = array();
	private $page = 0;
	private $row_index = -1;
	private $vars = array();
	private $order_link = array();

	public function __construct($id = null, $configs = array ()) {
		$this->id = $id != null ? $id : uniqid();

		foreach ($configs as $name => $value) {
			$this->{$name} = $value;
		}
	}

	public function add_header($title = null, $options = array ()) {
		$this->header[] = array(
			$title,
			$options
		);
		return $this;
	}

	public function set_order_link($url) {
		$this->order_link = $url;
		return $this;
	}

	public function set_vars($vars = array ()) {
		$this->vars = $vars;
		return $this;
	}

	public function next_row() {
		$this->row_index++;
		return $this;
	}

	public function add_attr_row() {
		$args = func_get_args();
		if (!is_array($this->att_rows[$this->row_index])) {
			$this->att_rows[$this->row_index] = array();
		}

		if (count($args) >= 2) {
			$this->att_rows[$this->row_index][$args[0]] = $args[1];
		}
		else if (count($args) == 1) {
			$this->att_rows[$this->row_index] = array_merge($this->att_rows[$this->row_index], (array)$args[0]);
		}

		return $this;
	}

	public function add_col($col = '', $atts = array ()) {
		if (!is_array($this->rows[$this->row_index])) {
			$this->rows[$this->row_index] = array();
		}

		$this->rows[$this->row_index][] = array(
			$col,
			$atts
		);
		$colspan = (int)$atts['colspan'];
		$this->row_colspan = max(count($this->row_rows[$this->row_index]) + $colspan - 1, $this->row_colspan);

		return $this;
	}

	public function add_col_checkbox($bool) {
		$check = $bool ? \Form::checkbox(null, null, true, array('disabled')) : \Form::checkbox(null, null, null, array('disabled'));

		$this->addCol($check, array(
			'width' => 20,
			'align' => 'center'
		));

		return $this;
	}

	public function add_act($act) {
		if (!is_array($this->action_rows[$this->row_index])) {
			$this->action_rows[$this->row_index] = array();
		}
		$this->action_rows[$this->row_index][] = $act;

		return $this;
	}

	public function add_act_edit($act = null) {
		$this->add_act(\UI\Tag::icon('pencil-2', array('href' => $act)));
	}

	public function add_act_delete($act = null) {
		$this->add_act(\UI\Tag::icon('trash', array('href' => $act)));
	}

	public function get_header_html() {
		$html = '';
		foreach ($this->header as $header) {
			list($title, $options) = $header;
			$atts = '';
			if (isset($options['atts'])) {
				$atts = $this->get_atts_html($options['atts']);
			}
			$html .= '<th ' . $atts . '>' . $title . '</th>';
		}

		return '<tr>' . $html . '</tr>';
	}

	private function get_attrs_html($atts = array ()) {
		if (count($atts) == 0)
			return '';

		$html = array();
		foreach ($atts as $key => $val) {
			$html[] = $key . '="' . $val . '"';
		}

		return implode(' ', $html);
	}

	private function get_scripts() {
		return "
			<script type=\"text/javascript\">
				(function($){
					$('#{$this->id} .checkall').on('click', function() {
						var checked = $(this).is(':checked');
						$('#{$this->id}').find('input[type=checkbox].check-item').not(this).each(function() {
							$(this).attr('checked', checked);
						});
					});
					$('.check-item').on('click', function() {
						var items_checked = $('#{$this->id} input[type=checkbox].check-item:checked').length;
						var items_length = $('#{$this->id} input[type=checkbox].check-item').length;
						if(items_checked == items_length) {
							$('#{$this->id}').attr('checked', true);
						}
					});
				})(jQuery);
			</script>";
	}

	public function get_html() {

	}

	public function show() {
		echo $this->get_html();
	}

}
